<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item  v-if="checkPermi(['platop'])" label="商户号" prop="userNo">
        <el-input
          v-model="queryParams.user_no"
          placeholder="请输入商户号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['assets:edit']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['assets:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['assets:edit']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['assets:query']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="assetsList" @selection-change="handleSelectionChange">
      <el-table-column v-if="checkPermi(['platop'])" label="商户号" align="center" prop="userNo" />
      <el-table-column  v-if="checkPermi(['platop'])" label="货币代码(平台)" align="center" prop="currency">
      </el-table-column>
      <el-table-column label="货币代码" align="center" prop="oriCurrency">
        <template slot-scope="scope">
            {{ scope.row.oriCurrency }}
        </template>
      </el-table-column>
      <el-table-column label="余额" align="center" prop="usable" >
        <template slot-scope="scope">
          <span style="color: rgb(21, 184, 108);">{{ scope.row.usable}}</span>
        </template>
      </el-table-column>
      <el-table-column label="冻结中余额" align="center" prop="frozen">
        <template slot-scope="scope">
          <span style="color: rgb(255, 104, 72);">{{ scope.row.frozen}}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="checkPermi(['assets:edit'])" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['assets:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改assets对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="变动金额" prop="usable">
          <el-input v-model="form.usable" placeholder="请输入变动金额" />
        </el-form-item>
        <el-form-item label="修改理由" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入修改理由" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAssets, getAssets, delAssets, addAssets, updateAssets } from "@/api/fusion/assets";

export default {
  name: "Assets",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // assets表格数据
      assetsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        usable: null,
        frozen: null,
        version: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // usable: [
        //   { required: true, message: "$comment不能为空", trigger: "blur" }
        // ],
        // frozen: [
        //   { required: true, message: "$comment不能为空", trigger: "blur" }
        // ],
        // version: [
        //   { required: true, message: "$comment不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询assets列表 */
    getList() {
      this.loading = true;
      listAssets(this.queryParams).then(response => {
        this.assetsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userNo: null,
        currency: null,
        usable: null,
        frozen: null,
        version: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map(item => item.userNo)
    //   this.single = selection.length!==1
    //   this.multiple = !selection.length
    // },
    // /** 新增按钮操作 */
    // handleAdd() {
    //   this.reset();
    //   this.open = true;
    //   this.title = "添加assets";
    // },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userNo = row.userNo || this.ids
      getAssets(userNo, row.currency).then(response => {
        response.data.usable = ''
        this.form = response.data;
        this.open = true;
        this.title = "修改钱包余额";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userNo != null) {
            updateAssets(this.form).then(response => {
              this.$modal.msgSuccess("修改钱包余额成功");
              this.open = false;
              this.getList();
            });
          } else {
            // addAssets(this.form).then(response => {
            //   this.$modal.msgSuccess("新增成功");
            //   this.open = false;
            //   this.getList();
            // });
          }
        }
      });
    },
    /** 删除按钮操作 */
    // handleDelete(row) {
    //   const userNos = row.userNo || this.ids;
    //   this.$modal.confirm('是否确认删除assets编号为"' + userNos + '"的数据项？').then(function() {
    //     return delAssets(userNos);
    //   }).then(() => {
    //     this.getList();
    //     this.$modal.msgSuccess("删除成功");
    //   }).catch(() => {});
    // },
    /** 导出按钮操作 */
    // handleExport() {
    //   this.download('assets/assets/export', {
    //     ...this.queryParams
    //   }, `assets_${new Date().getTime()}.xlsx`)
    // }
  }
};
</script>
